package com.xuxin.pda.activity;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.zyapi.pos.PosManager;
import android.zyapi.pos.PrinterDevice;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.xuxin.pda.R;
import com.xuxin.pda.barcode.BarcodeCreater;
import com.xuxin.pda.base.BaseActivity;
import com.xuxin.pda.bean.DptBean;
import com.xuxin.pda.bean.MemberBean;
import com.xuxin.pda.contract.PrintContract;
import com.xuxin.pda.listener.PrinterListener;
import com.xuxin.pda.popup.ConfirmPopup;
import com.xuxin.pda.presenter.PrintPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2021/2/8 0008
 */
public class PrintActivity extends BaseActivity implements PrintContract.View {
    private final PrintContract.Presenter presenter = new PrintPresenter(this);
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;

    @BindView(R.id.print_dpt)
    TextView print_dpt;
    @BindView(R.id.print_member)
    TextView print_member;
    @BindView(R.id.print_dpt_print)
    Button print_dpt_print;
    @BindView(R.id.print_member_print)
    Button print_member_print;
    @BindView(R.id.print_ck_print)
    Button print_ck_print;
    @BindView(R.id.print_box_print)
    Button print_box_print;

    private String dptID, memberID;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_print);
    }

    @Override
    public void initView() {
        title_name.setText("预设二维码");
        title_right.setVisibility(View.INVISIBLE);
    }

    @Override
    public void initData() {
        initPrint();
        handler.sendEmptyMessage(0);
        handler.sendEmptyMessage(1);
    }

    @Override
    public void onComplete() {

    }

    @Override
    @OnClick({R.id.title_left, R.id.print_dpt, R.id.print_member,
            R.id.print_dpt_print, R.id.print_member_print, R.id.print_ck_print, R.id.print_box_print})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.print_dpt) {
            if (null != DptList && null != dptBean && null != dptBean.getResult()) {
                new XPopup.Builder(context)
                        .maxHeight((int) (ScreenUtils.getScreenWidth() * 0.85))
                        .maxWidth((int) (ScreenUtils.getScreenWidth() * 0.65))
                        .asCenterList("请选择科室", DptList, null, 0,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        print_dpt.setText(text);
                                        dptID = dptBean.getResult().getItems().get(position).getDepartmentId();
                                    }
                                }, R.layout.popup_list, R.layout.popup_list_item)
                        .show();
            } else {
                ToastUtils.showShort("暂无相应科室");
            }
        } else if (id == R.id.print_member) {
            if (null != MemberList && null != memberBean && null != memberBean.getResult()) {
                new XPopup.Builder(context)
                        .maxHeight((int) (ScreenUtils.getScreenWidth() * 0.85))
                        .maxWidth((int) (ScreenUtils.getScreenWidth() * 0.65))
                        .asCenterList("请选择工作人员", MemberList, null, 0,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        memberID = memberBean.getResult().getItems().get(position).getENum();
                                        print_member.setText(text);
                                    }
                                }, R.layout.popup_list, R.layout.popup_list_item)
                        .show();
            } else {
                ToastUtils.showShort("暂无工作人员");
            }
        } else if (id == R.id.print_dpt_print) {
            String title = print_dpt.getText().toString();
            if (!StringUtils.isEmpty(title) && !StringUtils.isEmpty(dptID)) {
                new XPopup.Builder(context)
                        .dismissOnTouchOutside(true)
                        .dismissOnBackPressed(true)
                        .asCustom(new ConfirmPopup(context, "确定要打印科室二维码吗？", new ConfirmPopup.OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                print(title, dptID);
                            }
                        })).show();
            } else {
                ToastUtils.showShort("请选择科室");
            }

        } else if (id == R.id.print_member_print) {
            String title = print_member.getText().toString();
            if (!StringUtils.isEmpty(title) && !StringUtils.isEmpty(memberID)) {
                new XPopup.Builder(context)
                        .dismissOnTouchOutside(true)
                        .dismissOnBackPressed(true)
                        .asCustom(new ConfirmPopup(context, "确定要打印工号二维码吗？", new ConfirmPopup.OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                print(title, memberID);
                            }
                        })).show();
            } else {
                ToastUtils.showShort("请选择工作人员");
            }
        } else if (id == R.id.print_ck_print) {
            new XPopup.Builder(context)
                    .dismissOnTouchOutside(true)
                    .dismissOnBackPressed(true)
                    .asCustom(new ConfirmPopup(context, "确定要打印仓库二维码吗？", new ConfirmPopup.OnConfirmListener() {
                        @Override
                        public void onConfirm() {
                            String ckID = String.valueOf(System.currentTimeMillis());
                            print("仓库：" + ckID, "WHS" + ckID);
                        }
                    })).show();
        } else if (id == R.id.print_box_print) {
            new XPopup.Builder(context)
                    .dismissOnTouchOutside(true)
                    .dismissOnBackPressed(true)
                    .asCustom(new ConfirmPopup(context, "确定要打印箱子二维码吗？", new ConfirmPopup.OnConfirmListener() {
                        @Override
                        public void onConfirm() {
                            String ckID = String.valueOf(System.currentTimeMillis());
                            print("箱子：" + ckID, "BOX" + ckID);
                        }
                    })).show();
        }
    }

    private String[] DptList;
    private DptBean dptBean;

    @Override
    public void onSuccess(DptBean dptBean) {
        if (null != dptBean && null != dptBean.getResult()) {
            this.dptBean = dptBean;
            DptList = new String[dptBean.getResult().getItems().size()];
            for (int i = 0; i < dptBean.getResult().getItems().size(); i++) {
                DptList[i] = "科室："+dptBean.getResult().getItems().get(i).getDept_Name();
            }
        }
    }

    private String[] MemberList;
    private MemberBean memberBean;

    @Override
    public void onSuccess(MemberBean memberBean) {
        if (null != memberBean && null != memberBean.getResult()) {
            this.memberBean = memberBean;
            MemberList = new String[memberBean.getResult().getItems().size()];
            for (int i = 0; i < memberBean.getResult().getItems().size(); i++) {
                MemberList[i] = "员工："+memberBean.getResult().getItems().get(i).getName();
            }
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getDptList();
            } else if (msg.what == 1) {
                presenter.getMemberList();
            }
            return false;
        }
    });

    /**
     * 初始化打印
     */
    private PrinterDevice mPrinter = null;

    private void initPrint() {
        mPrinter = PosManager.get().getPrinterDevice();
        mPrinter.setPrintEventListener(new PrinterListener());
        mPrinter.init();
    }

    /**
     * 开始打印
     */
    private void print(String title, String message) {
        if (mPrinter.isPrinting()) {
            ToastUtils.showShort("正在打印中，请稍后...");
            return;
        }
        int concentration = 25;
        StringBuilder sb = new StringBuilder();
        sb.append("  ").append(title);
        sb.append("\n");
        PrinterDevice.TextData tData = mPrinter.new TextData();
        tData.addText(sb.toString());
        mPrinter.addText(concentration, tData);
        Bitmap mBitmap = BarcodeCreater.encode2dAsBitmap(message, 300, 300, 2);
        mPrinter.addBmp(concentration, 0, mBitmap);
        sb = new StringBuilder();
        sb.append("\n");
        sb.append("\n");
        sb.append("\n");
        sb.append("\n");
        sb.append("\n");
        tData = mPrinter.new TextData();
        tData.addText(sb.toString());
        mPrinter.addText(concentration, tData);
        mPrinter.printStart();
    }
}